package com.pkdl.dao;

import com.pkdl.dto.ChiTietBenhAnDTO;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

// Data access object, which get data of ChiTietBenhAn table from database
public class ChiTietBenhAnDAO {
	public ArrayList<ChiTietBenhAnDTO> selectAll() {
		ArrayList<ChiTietBenhAnDTO> list = new ArrayList<ChiTietBenhAnDTO>();
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect.prepareCall("{call usp_ChiTietBenhAn_SelectAll()}");
			ResultSet rs = cs.executeQuery();
			while (rs.next()) {
				ChiTietBenhAnDTO chitietbenhan = new ChiTietBenhAnDTO();
				chitietbenhan.setMaCTBA(rs.getInt("MaCTBA"));
				chitietbenhan.setMaBenhAn(rs.getInt("MaBenhAn"));
				chitietbenhan.setNgayKham(rs.getDate("NgayKham"));
				chitietbenhan.setTinhTrang(rs.getString("TinhTrang"));
				list.add(chitietbenhan);
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(ChiTietBenhAnDAO.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(ChiTietBenhAnDAO.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
		return list;
	}

	public ChiTietBenhAnDTO selectById(int mactba) {
		ChiTietBenhAnDTO chitietbenhan = new ChiTietBenhAnDTO();
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect.prepareCall("{call usp_ChiTietBenhAn_SelectByID(?)}");
			cs.setInt(1, mactba);
			ResultSet rs = cs.executeQuery();
			while (rs.next()) {
				chitietbenhan.setMaCTBA(rs.getInt("MaCTBA"));
				chitietbenhan.setMaBenhAn(rs.getInt("MaBenhAn"));
				chitietbenhan.setNgayKham(rs.getDate("NgayKham"));
				chitietbenhan.setTinhTrang(rs.getString("TinhTrang"));
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(ChiTietBenhAnDAO.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(ChiTietBenhAnDAO.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
		return chitietbenhan;
	}

	public boolean insert(ChiTietBenhAnDTO chitietbenhan) {
		boolean result = true;
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect.prepareCall("{call usp_ChiTietBenhAn_Insert(?, ?, ?)}");
			cs.setInt(1, chitietbenhan.getMaBenhAn());
			cs.setDate(2, new java.sql.Date(chitietbenhan.getNgayKham().getTime()));
			cs.setString(3, chitietbenhan.getTinhTrang());
			int n = cs.executeUpdate();
			if (n == 0) {
				result =false;
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(ChiTietBenhAnDAO.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(ChiTietBenhAnDAO.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
		return result;
	}

	public boolean update(ChiTietBenhAnDTO chitietbenhan) {
		boolean result = true;
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect.prepareCall("{call usp_ChiTietBenhAn_Update(?, ?, ?, ?)}");
			cs.setInt(1, chitietbenhan.getMaCTBA());
			cs.setInt(2, chitietbenhan.getMaBenhAn());
			cs.setDate(3, new java.sql.Date(chitietbenhan.getNgayKham().getTime()));
			cs.setString(4, chitietbenhan.getTinhTrang());
			int n = cs.executeUpdate();
			if (n == 0) {
				result =false;
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(ChiTietBenhAnDAO.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(ChiTietBenhAnDAO.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
		return result;
	}

	public boolean delete(int mactba) {
		boolean result = true;
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect.prepareCall("{call usp_ChiTietBenhAn_Delete(?)}");
			cs.setInt(1, mactba);
			int n = cs.executeUpdate();
			if (n == 0) {
				result = false;
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(ChiTietBenhAnDAO.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(ChiTietBenhAnDAO.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
		return result;
	}

	public ArrayList<ChiTietBenhAnDTO> selectByMaBenhAn(int mabenhan) {
		ArrayList<ChiTietBenhAnDTO> list = new ArrayList<ChiTietBenhAnDTO>();
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect.prepareCall("{call usp_ChiTietBenhAn_SelectByMaBenhAn(?)}");
			cs.setInt(1, mabenhan);
			ResultSet rs = cs.executeQuery();
			while (rs.next()) {
				ChiTietBenhAnDTO chitietbenhan = new ChiTietBenhAnDTO();
				chitietbenhan.setMaCTBA(rs.getInt("MaCTBA"));
				chitietbenhan.setMaBenhAn(rs.getInt("MaBenhAn"));
				chitietbenhan.setNgayKham(rs.getDate("NgayKham"));
				chitietbenhan.setTinhTrang(rs.getString("TinhTrang"));
				list.add(chitietbenhan);
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(ChiTietBenhAnDAO.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(ChiTietBenhAnDAO.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
		return list;
	}

}
